package com.cskaoyan.demo7.servlet;

import com.cskaoyan.bean.Region;
import com.cskaoyan.demo7.mapper.RegionMapper;
import com.cskaoyan.util.MyBatisUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

// 访问请求 localhost:8080/demo7/region/list    → 在控制台打印 region列表(数据库market_region表)
@WebServlet("/region/list")
public class RegionListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RegionMapper regionMapper = MyBatisUtils.getRegionMapper();
        List<Region> regions = regionMapper.selectRegionList();
        ObjectMapper objectMapper = new ObjectMapper();

//        for (Region region : regions) {
//
//            String s = objectMapper.writeValueAsString(region);
//
//            System.out.println("s = " + s);
//            resp.getWriter().println(s);
//        }

        regions.forEach(
                region -> {
                    try {
                        resp.getWriter().println(region);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
        );



    }
}
